﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class CoverService : ICoverService
    {

        private ICoverManager _ic;
        public CoverService(ICoverManager ic)
        {
            _ic = ic;
        }

        public async Task<int> AddCoverAsync(string details, string image1, string image2, string image3)
        {
            return await _ic.AddAsync(new Model.Cover 
            {
                Details = details,
                Image1 = image1,
                Image2 = image2,
                Image3 = image3,
            });
        }

        public async Task<int> DeleteCoverAsync(int id)
        {
            var data = await _ic.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ic.DeleteAsync(data);
        }

        public async Task<List<CoverDto>> GetAllCoverAsync(int deleteId = 1)
        {
            var data = await _ic.GetChooseAsync(cov => cov.DeleteId == deleteId);
            return data == null ? new List<CoverDto>() : data.Select(cov => new CoverDto 
            {
                Id = cov.Id,
                DeleteId = cov.DeleteId,
                Image1 = cov.Image1,
                Image2 = cov.Image2,
                Image3 = cov.Image3,

            }).ToList();
        }

        public async Task<CoverDto> GetBlogCoverId(string image1, int deleteId = 1)
        {
            var data = await _ic.GetChooseAsync(cb => cb.Image1.Equals(image1));
            return data == null ? new CoverDto() : new CoverDto
            {
                Id = data[0].Id
            };
        }

        public async Task<CoverDto> GetByIdAsync(int id)
        {
            var data = await _ic.GetById(id);
            return data == null ? new CoverDto() : new CoverDto 
            {
                Id = data.Id,
                DeleteId = data.DeleteId,
                Image1 = data.Image1,
                Image2 = data.Image2,
                Image3 = data.Image3,

            };
        }

        public async Task<List<CoverDto>> GetByNameAsync(string details, int deleteId = 1)
        {
            var data = await _ic.GetChooseAsync(cov => cov.Details.Contains(details) && cov.DeleteId == deleteId);
            return data == null ? new List<CoverDto>() : data.Select(cov => new CoverDto 
            {
                Id = cov.Id,
                DeleteId = cov.DeleteId,
                Image1 = cov.Image1,
                Image2 = cov.Image2,
                Image3 = cov.Image3,

            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ic.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ic.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ic.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ic.UpdateAsync(data);
        }

        public async Task<int> UpdateCoverAsync(int id, string details, string image1, string image2, string image3)
        {
            var data = await _ic.GetById(id);
            if(data == null)
                return 0;
            data.Details = details;
            data.Image1 = image1;
            data.Image2 = image2;
            data.Image3 = image3;
            return await _ic.UpdateAsync(data);
        }
    }
}
